/** 格式化相关工具集 */

/**
 * 格式化字节显示
 *
 * @param bytes 字节
 * @param decimals 保留小数位数
 * @returns
 */
export const formatBytes = (bytes: unknown, decimals = 2): string => {
  const realBytes = parseFloat(bytes as string)
  if (bytes === 0) return '0 Bytes'

  const k = 1024
  const dm = decimals < 0 ? 0 : decimals
  const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']

  const i = Math.floor(Math.log(realBytes) / Math.log(k))

  return parseFloat((realBytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}
